/*
 * Copyright 2012 Quytelda Gaiwin
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.tamalin.panthersleek;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The Panther Sleek main class.
 * @author Quytelda K. Gaiwin
 */
public class Main
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        if (args.length > 0)
        {
            /* Parse the command line arguments. */
            for (int i = 0; i < args.length; i++)
            {
                if ((args[i]).equals("-h") || args[i].equals("--help"))
                {
                    // print help
                    printHelp();
                    return;
                }
                else if (args[i].equals("-a") || args[i].equals("--algorithm"))
                {
                    int next = ++i;
                    if (args[next].equals("-c"))
                    {
                        String cipher = args[++i];
                        Panther.setAlgorithm(cipher);
                    }
                    else if (args[next].equals("-f") || args[next].equals("-d"))
                    {
                        String digest = args[++i];
                        Panther.setDigestAlgorithm(digest);
                    }
                    else
                    {
                        System.err.println(args[next] + " is not a valid argument for algorithm type.\n\tTry panthersleek -h for details.");
                        Logger.getLogger(Main.class.getName()).log(Level.WARNING, "Invalid algorithm type ({0}) provided.  Ignoring algorithm parameter.", args[next]);
                    }
                }
                else if (args[i].equals("-v") || args[i].equals("--verbose"))
                {
                    // Set program in verbose mode
                    Logger.getLogger(Panther.class.getName()).setLevel(Level.ALL);
                }
                else if (args[i].equals("-V") || args[i].equals("--version"))
                {
                    printVersion(true);
                    return;
                }
                else
                {
                    if (!(args[i].equals("-l") || args[i].equals("--language") || args[i].equals("en") || args[i].equals("zh")))
                    {
                        System.err.println("Unrecognized argument: " + args[i]);
                        System.err.println("Try \"panthersleek -h\" for details.");
                        System.exit(1);
                    }
                }
            }
        }

        /* Configure for use on Mac OS X. */
        if (System.getProperty("os.name").contains("Mac"))
        {
            System.setProperty("apple.laf.useScreenMenuBar", "true");
            System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Panther Sleek");
        }

        Panther panther = new Panther(args);

        /*
         * Size the window to 1/2 the dimensions of the screen.
         */
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int scrnWidth = (toolkit.getScreenSize().width / 2);
        int scrnHeight = (toolkit.getScreenSize().height / 2);
        panther.setSize(scrnWidth, scrnHeight);

        // Center the window in the display.
        int ww = panther.getWidth();
        int wh = panther.getHeight();
        Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
        int sw = ss.width;
        int sh = ss.height;
        int x = (sw / 2) - (ww / 2);
        int y = (sh / 2) - (wh / 2);

        panther.setLocation(x, y);

        /* Show the program's main window. */
        panther.setVisible(true);
        panther.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    }

    /**
     * Prints help/usage message to console.  This method is invoked when the help arguments
     * are provided or when there is an invalid argument provided.
     */
    public static void printHelp()
    {
        URL url = Main.class.getResource("/org/tamalin/panthersleek/resources/international/help_" + Locale.getDefault().getLanguage() + ".txt");
        try
        {
            URLConnection urlc = url.openConnection();
            Scanner scanner = new Scanner(urlc.getInputStream());
            while (scanner.hasNextLine())
            {
                System.out.println(scanner.nextLine());
            }
        }
        catch (IOException ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Unable to open help stream.", ex);
        }

    }

    public static void printVersion(boolean includeNotice)
    {
        System.out.println("Panther Sleek " + Panther.VERSION);
        System.out.println("Copyright (C) Quytelda Gaiwin\n");

        if (includeNotice)
        {
            System.out.println("This software is available, free of charge, under the terms of the\n"
                    + "Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0).\n");
        }
    }
}